// pages/admin-type-info/admin-type-info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    default: '请选择',
    hiddenmodalput: true,

  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)

    let index = e.detail.value
    let array = this.data.arrxx
    this.setData({
      index: index,
      default: array[index].name
    })
  },


  type_edit: function (e) {
    var id = e.currentTarget.dataset.id
    this.setData({
      id: id,
      hiddenmodalput: !this.data.hiddenmodalput
    })

  },
  cancelM: function (e) {
    this.setData({
      hiddenmodalput: true,
    })
  },

  confirmM: function (e) {
    var key = this.data.arrxx[this.data.index].name;
    // console.log(key)
    // console.log(this.data.id)

    var that = this;
    wx.request({
      url: 'http://localhost/shopping/admin_type_th.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        id: that.data.id,
        key: key
      },
      success: function (res) {
        // console.log(res.data);
        console.log('修改分类成功！')
        that.Type();

        that.setData({})
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    that.setData({
      hiddenmodalput: true
    })

  },
  type_del: function (e) {
    var id = e.currentTarget.dataset.id
    // console.log(id)
    let key = this.data.arrxx[this.data.index].name;
    // console.log(key)
    let that = this;

    wx.showModal({
      title: '',
      content: '是否确认删除',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')

          wx.request({
            url: 'http://localhost/shopping/admin_type_del2.php',
            header: {
              'Content-Type': 'application/json'
            },
            data: {
              id: id,
              name: key
            },
            success: function (res) {
              // console.log(res.data);
              console.log('删除分类成功！')
              that.Type();
              that.setData({})
            },
            fail: function (res) {
              console.log("fail");
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  Type: function () {
    var that = this;
    var name = that.data.name
    wx.request({
      url: 'http://localhost/shopping/admin_type_info.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: name
      },
      success: function (res) {
        // console.log(res.data)
        that.setData({
          arrbb: res.data.info
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/admin_type.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data);
        that.setData({
          arrxx: res.data.type,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var id = e.id
    var name = e.name

    this.setData({
      id: id,
      name: name,
    })
    this.Type();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})